<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 最大时间（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给定一个数组&#xff0c;里面有 6 个整数&#xff0c;求这个数组能够表示的最大 24 进制的时间是多少&#xff0c;输出这个时间&#xff0c;无法表示输出 invalid。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入为一个整数数组&#xff0c;数组内有六个整数。</p> 
<p>输入整数数组长度为 6&#xff0c;不需要考虑其它长度&#xff0c;元素值为 0 或者正整数&#xff0c;6 个数字每个数字只能使用一次。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出为一个 24 进制格式的时间&#xff0c;或者字符串”invalid“。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">[0,2,3,0,5,6]</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">23:56:00</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题可以使用深度优先搜索DFS求解全排列&#xff0c;当然求解过程中需要过滤掉不合法的时间排列&#xff0c;然后剩下只需要进行默认的字典序升序后&#xff0c;获取最后一个时间排列就是最大的时间</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const arr &#61; JSON.parse(line);

  const regExp &#61; /(([01][0-9])|([2][0-3]))([0-5][0-9])([0-5][0-9])/;

  let dfs &#61; (arr, used, path, res) &#61;&gt; {
    if (path.length &#61;&#61;&#61; arr.length) {
      if (regExp.test(path.join(&#34;&#34;))) res.push([...path]);
      return;
    }

    for (let i &#61; 0; i &lt; arr.length; i&#43;&#43;) {
      if (!used[i]) {
        path.push(arr[i]);
        used[i] &#61; true;
        dfs(arr, used, path, res);
        used[i] &#61; false;
        path.pop();
      }
    }
  };

  const res &#61; [];
  dfs(arr, arr.slice().fill(false), [], res);

  if (!res.length) return console.log(&#34;invalid&#34;);

  const max &#61; res.sort().at(-1);
  console.log(&#96;${max[0]}${max[1]}:${max[2]}${max[3]}:${max[4]}${max[5]}&#96;);
});
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    String s &#61; sc.nextLine();
    Integer[] arr &#61;
        Arrays.stream(s.substring(1, s.length() - 1).split(&#34;,&#34;))
            .map(Integer::parseInt)
            .toArray(Integer[]::new);

    System.out.println(getResult(arr));
  }

  static Pattern c &#61; Pattern.compile(&#34;(([01][0-9])|([2][0-3])):([0-5][0-9]):([0-5][0-9])&#34;);

  public static String getResult(Integer[] arr) {
    ArrayList&lt;String&gt; res &#61; new ArrayList&lt;&gt;();
    dfs(arr, new boolean[arr.length], new LinkedList&lt;&gt;(), res);

    if (res.size() &#61;&#61; 0) return &#34;invalid&#34;;
    res.sort((a, b) -&gt; b.compareTo(a));
    return res.get(0);
  }

  public static void dfs(
      Integer[] arr, boolean[] used, LinkedList&lt;Integer&gt; path, ArrayList&lt;String&gt; res) {
    if (path.size() &#61;&#61; arr.length) {
      Integer[] t &#61; path.toArray(new Integer[0]);
      String time &#61; t[0] &#43; &#34;&#34; &#43; t[1] &#43; &#34;:&#34; &#43; t[2] &#43; &#34;&#34; &#43; t[3] &#43; &#34;:&#34; &#43; t[4] &#43; &#34;&#34; &#43; t[5];

      if (c.matcher(time).matches()) res.add(time);
      return;
    }

    for (int i &#61; 0; i &lt; arr.length; i&#43;&#43;) {
      if (!used[i]) {
        path.add(arr[i]);
        used[i] &#61; true;
        dfs(arr, used, path, res);
        used[i] &#61; false;
        path.removeLast();
      }
    }
  }
}
</code></pre> 
<p> </p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python">import re

# 输入获取
arr &#61; eval(input())

p &#61; re.compile(&#34;(([01][0-9])|([2][0-3]))([0-5][0-9])([0-5][0-9])&#34;)


def dfs(arr, used, path, res):
    if len(path) &#61;&#61; len(arr):
        tmp &#61; &#34;&#34;.join(map(str, path))
        if p.match(tmp):
            res.append(path[:])
        return

    for i in range(len(arr)):
        if not used[i]:
            path.append(arr[i])
            used[i] &#61; True
            dfs(arr, used, path, res)
            used[i] &#61; False
            path.pop()


# 算法入口
def getResult():
    res &#61; []
    dfs(arr, [False] * (len(arr)), [], res)

    if len(res) &#61;&#61; 0:
        return &#34;invalid&#34;

    res.sort()
    t &#61; res[-1]

    return f&#34;{t[0]}{t[1]}:{t[2]}{t[3]}:{t[4]}{t[5]}&#34;


# 算法调用
print(getResult())
</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>